﻿Imports System.Data.SqlClient

Public Class LoaiHangDAO
    Public Shared Function Insert(ByVal lh As LoaiHangDTO) As Integer
        Dim str = String.Format("Insert into LOAIHANG(MaLoai,TenLoai,MoTa,MaNhaSX,MaXX,BaoHanh) values ('{0}',N'{1}',N'{2}',N'{3}',N'{4}',N'{5}')", lh.MaLoai, lh.TenLoai, lh.MoTa, lh.NhaSanXuat, lh.XuatXu, lh.BaoHanh)
        Dim connect As SqlConnection = DataProvider.ConnectDB("QuanLyHangHoa")
        connect.Open()
        Dim cmd As New SqlCommand(str, connect)
        cmd.CommandType = CommandType.Text
        Dim result As Integer = 1
        Try
            cmd.ExecuteNonQuery()
        Catch ex As Exception
            result = 0
        End Try

        Return result
    End Function

    Public Shared Function Delete(ByVal maLoai As String) As Integer
        Dim result As Integer = 1
        Dim StrSql As String
        StrSql = "Delete from LOAIHANG where MaLoai = '" & maLoai & "'"
        Try
            SQLProvider.ExecNoneQuery(StrSql)
        Catch ex As Exception
            result = 0
        End Try
        Return result
    End Function

    Public Shared Function Update(ByVal lh As LoaiHangDTO) As Integer
        Dim result As Integer = 1
        Dim StrSql As String
        StrSql = "Update LOAIHANG set TenLoai = N'" & lh.TenLoai & "',MoTa = N'" & lh.MoTa & "',MaNhaSX = '" & lh.NhaSanXuat & "',MaXX = '" & lh.XuatXu & "'" & " Where MaLoai = '" & lh.MaLoai & "'"
        Try
            SQLProvider.ExecNoneQuery(StrSql)
        Catch ex As Exception
            result = 0
        End Try
        Return result
    End Function

    Public Shared Function Select_All() As DataTable
        Dim dt As DataTable
        Dim StrSql As String
        StrSql = "Select * from LOAIHANG"
        dt = SQLProvider.ExecQuery(StrSql)
        Return dt
    End Function

    Public Shared Function Select_By_Ma(ByVal maLoai As String) As DataTable
        Dim dt As DataTable
        Dim StrSql As String
        StrSql = "Select * from LOAIHANG where MaLoai = '" & maLoai & "'"
        dt = SQLProvider.ExecQuery(StrSql)
        Return dt
    End Function

End Class
